package cn.legend.travel.destination.main.service.impl;


import cn.legend.travel.common.enumerator.ServiceCode;
import cn.legend.travel.common.exception.ServiceException;
import cn.legend.travel.common.pojo.po.DistrictSimplePO;
import cn.legend.travel.common.pojo.po.SceneriesPO;
import cn.legend.travel.common.pojo.po.SceneryPO;
import cn.legend.travel.destination.main.dao.cache.repository.ISceneryCacheRepository;
import cn.legend.travel.destination.main.dao.persist.repository.IDistrictRepository;
import cn.legend.travel.destination.main.dao.persist.repository.ISceneryRepository;
import cn.legend.travel.destination.main.dao.search.SceneryElasticsearchRepository;
import cn.legend.travel.destination.main.pojo.vo.CountInt;
import cn.legend.travel.destination.main.pojo.vo.DistrictVO;
import cn.legend.travel.destination.main.pojo.vo.SceneryVO;
import cn.legend.travel.destination.main.service.IDistrictService;
import cn.legend.travel.destination.main.service.ISceneryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 处理省市区数据的业务实现类
 *
 * @author java@tedu.cn
 * @version 1.0
 */
@Slf4j
@Service
public class SceneryServiceImpl implements ISceneryService {
    @Autowired
    private IDistrictRepository districtRepository;
    @Autowired
    private ISceneryRepository sceneryRepository;
    @Autowired
    private ISceneryCacheRepository sceneryCacheRepository;
    @Autowired
    private IDistrictService service;
    @Autowired
    private SceneryElasticsearchRepository sceneryElasticsearchRepository;

    public SceneryServiceImpl() {
        log.debug("创建业务类对象：SceneryServiceImpl");
    }

    @Override
    public List<SceneryVO> list() {
        return null;
    }

    @Override
    public List<SceneriesPO> selectNearbyAttractions(String name) {
        List<SearchHit<DistrictVO>> searchHits = service.selectNearbyAttractions(name);
        List<SceneriesPO> sceneriesList = new ArrayList<>();
        for (SearchHit<DistrictVO> searchHit : searchHits) {
            DistrictVO districtVO = searchHit.getContent();
            SceneriesPO sceneriesPO = new SceneriesPO();
            if (districtVO.getName().equals(name)) {
                continue;
            }
            List<SceneryPO> sceneryList = sceneryRepository.selectByDestinationId(districtVO.getId());
            BeanUtils.copyProperties(districtVO, sceneriesPO);
            sceneriesPO.setList(sceneryList);
            sceneriesList.add(sceneriesPO);
        }
        return sceneriesList;
    }

    @Override
    public List<SceneryPO> selectScenery(String name) {
        DistrictVO districtVO = districtRepository.selectListByName(name);
        return sceneryRepository.selectByDestinationId(districtVO.getId());
    }

    @Override
    public CountInt selectCount(String name) {
        DistrictVO districtVO = districtRepository.selectListByName(name);
        System.out.println(districtVO);
        int countDistrict = sceneryRepository.selectCountDistrict(districtVO.getId());
        int countDelicacy = sceneryRepository.selectCountDelicacy(districtVO.getId());
        int countHotel = sceneryRepository.selectCountHotel(districtVO.getId());
        CountInt countInt = new CountInt();
        countInt.setDelicacyCount(countDelicacy);
        countInt.setSceneryCount(countDistrict);
        countInt.setHotelCount(countHotel);
        countInt.setName(name);
        return countInt;
    }

    @Override
    public void rebuildScenery() {
        List<DistrictSimplePO> districtSimpleList = districtRepository.listFromScenery();
        for (DistrictSimplePO districtSimplePO : districtSimpleList) {
            List<SceneryPO> sceneryList = sceneryRepository.selectByDestinationId(districtSimplePO.getId());
            sceneryCacheRepository.saveListByDestinationId(districtSimplePO.getId(), sceneryList);
        }
    }

    @Override
    public void elasticsearchScenery() {
        sceneryElasticsearchRepository.deleteAll();
        List<SceneryVO> sceneryVOS = sceneryRepository.selectAll();
        sceneryElasticsearchRepository.saveAll(sceneryVOS);
    }

    @Override
    public List<SceneryVO> selectByKeyword(String keyword) {
        Pageable pageable = PageRequest.of(0, 8);
        SearchPage<SceneryVO> searchHits = sceneryElasticsearchRepository.customSearch(keyword, pageable);
        List<SearchHit<SceneryVO>> content = searchHits.getContent();
        if (content.size() == 0) {
            String message = "查询失败，请输入正确的关键词";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        List<SceneryVO> sceneryVOS = new ArrayList<>();
        for (SearchHit<SceneryVO> sceneryVOSearchHit : content) {
            SceneryVO sceneryVO = sceneryVOSearchHit.getContent();
            sceneryVOS.add(sceneryVO);
        }
        return sceneryVOS;
    }
//根据景点名称查询附近景点
    @Override
    public List<SceneryVO> selectAttractions(String name) {
        Pageable pageable = PageRequest.of(0, 8);
        String distance = "200km";
        SearchPage<SceneryVO> searchHits = sceneryElasticsearchRepository.customSearchs(name, distance, pageable);
        List<SearchHit<SceneryVO>> hitsContent = searchHits.getContent();
        if (hitsContent.size() == 0) {
            return null;
        }
        List<SceneryVO> sceneryVOS = new ArrayList<>();
        for (SearchHit<SceneryVO> sceneryVOSearchHit : hitsContent) {
            SceneryVO sceneryVO = sceneryVOSearchHit.getContent();
            sceneryVOS.add(sceneryVO);
        }
        return sceneryVOS;
    }

}
